#!/usr/bin/env bash
#  系统级保活
# /system/bin/sh

file_dir="/data/local/tmp"

CopyFile() {
        cp -rf $file_dir/NativeDemo /system/bin/
        chmod 755 /system/bin/NativeDemo

        cp $file_dir/lib*.so /system/lib64/
}

CheckIfCopyFile() {
        if [ -s $file_dir ]; then
                echo "file exist"
                CopyFile
        else
                echo "file not exist"
        fi
}

Start() {
        nohup NativeDemo &
}

KeepAlive() {
        while true; do
                echo "check NativeDemo proc"
                proc_count=`ps | grep NativeDemo | wc -l`
                if [ $proc_count -le 0 ]; then
                        echo "start NativeDemo"
                        Start
                else
                        echo "NativeDemo already started"
                fi
                sleep 5
        done
}

main() {
        CheckIfCopyFile
        KeepAlive
}

main
